package com.atguigu.team.service;

import com.atguigu.team.domain.*;

import static com.atguigu.team.service.Data.*;
public class NameListService {
    private Employee[]  employees;
    public NameListService(){
        employees=new Employee[Data.EMPLOYEES.length];
        for(int i=0;i<employees.length;i++){
            int type=Integer.parseInt(EMPLOYEES[i][0]);
            //获取Employee的4个基本信息
            int id=Integer.parseInt(EMPLOYEES[i][1]);
            String name=EMPLOYEES[i][2];
            int age=Integer.parseInt(EMPLOYEES[i][3]);
            int salary=Integer.parseInt(EMPLOYEES[i][4]);
            Equipment equipment;
            double bonus;
            switch(type){
                case EMPLOYEE:
                    employees[i]=new Employee(id,name,age,salary);
                    break;
                case PROGRAMMER:
                    equipment=creatEquipment(i);
                    employees[i]=new Programmer(equipment,age,name,salary,id);
                    break;
                case DESIGNER:
                    equipment=creatEquipment(i);
                    bonus=Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i]=new Designer(id,name,salary,bonus,equipment,age);
                    break;
                case ARCHITECT:
                    int stock=Integer.parseInt(EMPLOYEES[i][6]);
                    equipment=creatEquipment(i);
                    bonus=Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i]=new Architect(id,name,salary,bonus,equipment,age,stock);
                    break;
            }
        }
    }

    private Equipment creatEquipment(int i) {
         switch(Integer.parseInt(EQUIPMENTS[i][0])){
             case PC:
                 return new PC(EQUIPMENTS[i][1],EQUIPMENTS[i][2]);
             case NOTEBOOK:
                 return new NoteBook(EQUIPMENTS[i][1],Integer.parseInt(EQUIPMENTS[i][2]));
             case PRINTER:
                 return new Printer(EQUIPMENTS[i][1],EQUIPMENTS[i][2]);
         }
         return null;
    }

    public Employee[] getAllEmployees() {
        return employees;
    }
    public Employee getEmployee(int id)throws TeamException{
        for(int i=0;i<employees.length;i++){
            if(employees[i].getId()==id) {
                return employees[i];
            }
        }
        throw new TeamException("找不到指定员工");
    }
}
